#include "GLApplication.h"

using namespace OpenGLWrapper;

GLApplication::GLApplication()
{
	_currentWidth = 0;
	_currentHeight = 0;

	_translateX = 0;
	_translateY = 0;
	_translateZ = -1000;

	_rotationX = 25;
	_rotationY = -25;
	_rotationZ = 0;

	_mousePanning = false;
	_startXPan = 0;
	_startYPan = 0;
	_scaleValue = 1.0;
	_defaultFontSize = 15;
	_fontSize = _defaultFontSize;
}

bool GLApplication::Init()
{
	glEnable( GL_POINT_SMOOTH );
	glEnable( GL_LINE_SMOOTH );
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	glEnable(GL_DEPTH_TEST);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	return true;
}

void GLApplication::Prepare(float dt)
{
	
}

void GLApplication::Render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glScaled(_scaleValue, _scaleValue, 1.0);
	glTranslatef(_translateX, _translateY, 0.0);
	_fontSize = _defaultFontSize / _scaleValue;
}

void GLApplication::Shutdown()
{

}

void GLApplication::OnResize(int width, int height)
{
	_currentWidth = width;
	_currentHeight = height;
}

void GLApplication::OnKeyDown(BYTE key)
{
	if (key == VK_ESCAPE)
		exit(0);
	if (key == VK_UP)
		_translateY -= 10.0f / _scaleValue;
	if (key == VK_DOWN)
		_translateY += 10.0f / _scaleValue;
	if (key == VK_LEFT)
		_translateX += 10.0f / _scaleValue;
	if (key == VK_RIGHT)
		_translateX -= 10.0f / _scaleValue;
	if (key == 'W' || key == 'w')
		_translateZ += 10.0f / _scaleValue;
	if (key == 'S' || key == 's')
		_translateZ -= 10.0f / _scaleValue;

	if (key == 'Q' || key == 'q')
		_rotationX += 1.0f;
	if (key == 'E' || key == 'e')
		_rotationX -= 1.0f;
	if (key == 'A' || key == 'a')
		_rotationY += 1.0f;
	if (key == 'D' || key == 'd')
		_rotationY -= 1.0f;
	if (key == 'Z' || key == 'z')
		_rotationZ += 1.0f;
	if (key == 'C' || key == 'c')
		_rotationZ -= 1.0f;

	if (abs(_rotationX) > 360)
		_rotationX = 0;
	if (abs(_rotationY) > 360)
		_rotationY = 0;
	if (abs(_rotationZ) > 360)
		_rotationZ = 0;
}

void GLApplication::OnMouseDown(int button, int x, int y)
{
	if (button == 1)
	{
		_mousePanning = true;
		_startXPan = x;
		_startYPan = y;
	}
}

void GLApplication::OnMouseMove(int x, int y)
{
	if (_mousePanning)
	{
		int dx = x - _startXPan;
		int	dy = y - _startYPan;

		if (GetKeyState(VK_LMENU) & 0x80)
		{
			_rotationY += dx;
			_rotationX += dy;
		}
		else
		{
			_translateX += dx / _scaleValue;
			_translateY -= dy / _scaleValue;					
		}

		_startXPan = x;
		_startYPan = y;

		if (abs(_rotationX) > 360)
			_rotationX = 0;
		if (abs(_rotationY) > 360)
			_rotationY = 0;
	}
}

void GLApplication::OnMouseUp(int button, int x, int y)
{
	if (button == 1)
		_mousePanning = false;
}

void GLApplication::OnMouseWheel(int distance, int x, int y)
{
	if (distance > 1)
		_scaleValue += 0.1f;
	else
		_scaleValue -= 0.1f;

	if (_scaleValue <= 0.3f)
		_scaleValue = 0.3f;	
}

GLApplication::~GLApplication()
{
}
